package chaowan.Service.impl;

import chaowan.Mapper.GoodsMapper;
import chaowan.Mapper.OrderDetailMapper;
import chaowan.Mapper.OrderMainMapper;
import chaowan.Mapper.UserCartMapper;
import chaowan.Service.OrderMainService;
import chaowan.pojo.Goods;
import chaowan.pojo.OrderDetail;
import chaowan.pojo.OrderMain;
import chaowan.pojo.UserCart;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderMainServiceImpl implements OrderMainService {

    @Resource
    private OrderMainMapper orderMainMapper;

    @Override
    public List<OrderMain> getAllOrders() {
        return orderMainMapper.selectAll();
    }

    @Override
    public OrderMain getOrderById(Long oid) {
        return orderMainMapper.selectById(oid);
    }

    @Override
    public int save(OrderMain orderMain) {
        return orderMainMapper.insert(orderMain);
    }

    @Override
    public int update(OrderMain orderMain) {
        return orderMainMapper.update(orderMain);
    }

    @Override
    public int deleteById(Long oid) {
        OrderMain order = orderMainMapper.selectById(oid);
        if (order != null && ("已完成".equals(order.getStatus()) || "已取消".equals(order.getStatus()))) {
            orderDetailMapper.deleteByOrderId(oid);
            return orderMainMapper.deleteById(oid);
        }
        return 0;
    }

    @Override
    public List<OrderMain> getOrdersByUserId(Long uid) {
        return orderMainMapper.findByUserId(uid);
    }

    @Autowired
    private UserCartMapper userCartMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Override
    @Transactional
    public boolean checkoutOrder(Long userId, List<Long> cartIds) {
        List<UserCart> carts = userCartMapper.selectBatchIds(cartIds);

        if (carts == null || carts.isEmpty()) return false;

        BigDecimal total = BigDecimal.ZERO;
        List<OrderDetail> detailList = new ArrayList<>();

        for (UserCart cart : carts) {
            Goods goods = goodsMapper.selectById(cart.getGoodsId());
            if (goods == null) continue;

            OrderDetail detail = new OrderDetail();
            detail.setGoodsId(goods.getGid());
            detail.setQty(cart.getCount());
            detail.setPrice(goods.getPrice());
            detailList.add(detail);

            total = total.add(goods.getPrice().multiply(BigDecimal.valueOf(cart.getCount())));
        }

        if (detailList.isEmpty()) return false;

        OrderMain order = new OrderMain();
        order.setUid(userId);
        order.setTotal(total);
        order.setStatus("已下单");
        order.setReceiveInfo("用户默认地址");
        order.setCreated(new Date());
        orderMainMapper.insert(order);

        for (OrderDetail detail : detailList) {
            detail.setOrderId(order.getOid());
            orderDetailMapper.insert(detail);
        }

        userCartMapper.deleteBatchIds(cartIds);

        return true;
    }
}